package cn.lsj.payment.api;

import cn.lsj.common.Commons;
import cn.lsj.common.HttpStatus;
import cn.lsj.common.ServiceNameConstant;
import cn.lsj.common.entity.Payment;
import cn.lsj.payment.config.FeignConfiguration;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @className: PaymentFeigin
 * @description:
 * @author: liangshijie
 * @date: 2023/5/7
 **/
@FeignClient(name = ServiceNameConstant.PAYMENT, configuration = FeignConfiguration.class ,fallbackFactory = PaymentClient.PaymentClientFallbackFactory.class)
@RequestMapping(ServiceNameConstant.PAYMENT + Commons.SERVER_VERSION)
public interface PaymentClient {

    @GetMapping("/get/{id}")
    ResponseEntity<Payment> payment(@PathVariable("id") Integer id);

    @GetMapping("/loadbalancer")
    ResponseEntity<String> loadbalancer();

    @GetMapping("/timeout")
    ResponseEntity<Payment> timeout();

    @GetMapping("/gzip")
    ResponseEntity<List<Payment>> gzip();

    class PaymentClientFallback implements PaymentClient{

        @Override
        public ResponseEntity<Payment> payment(Integer id) {
            return ResponseEntity.status(HttpStatus.FUSING).build();
        }

        @Override
        public ResponseEntity<String> loadbalancer() {
            return ResponseEntity.status(HttpStatus.FUSING).build();
        }

        @Override
        public ResponseEntity<Payment> timeout() {
            return ResponseEntity.status(HttpStatus.FUSING).build();
        }

        @Override
        public ResponseEntity<List<Payment>> gzip() {
            return ResponseEntity.status(HttpStatus.FUSING).build();
        }
    }

    @Component
    class PaymentClientFallbackFactory implements FallbackFactory<PaymentClientFallback> {

        @Override
        public PaymentClientFallback create(Throwable cause) {
            cause.printStackTrace();
            return new PaymentClientFallback();

        }
    }
}
